

#####################
### Load Packages ###
#####################

library(qdapRegex)

# Set this Working Directory to the Folder with the Replication Files
#setwd("C:/Steven/GDrive/Research Files/Clark-Rogers/Descriptive Representation/APSR Final Submission/Replication")

###
# Sexism
###

CreatePlotBin <- function(CurrentFile, Sexism)
{
  colnames(CurrentFile) <- c("X", "Y")
  Estimates <- subset(CurrentFile, CurrentFile$X!="=")
  
  CIs <- subset(CurrentFile, CurrentFile$X=="=")
  LowerBounds <- unlist(rm_between(CIs$Y, '=[', ',', extract=TRUE))
  UpperBounds <- unlist(rm_between(CIs$Y, ',', ']', extract=TRUE))
  
  CurrentPlotBin <- data.frame(matrix(NA, 1,0))
  CurrentPlotBin$Sexism <- Sexism/100
  CurrentPlotBin$Estimate <- head(Estimates$Y,1)
  CurrentPlotBin$Estimate <- as.numeric(gsub("=", "", CurrentPlotBin$Estimate))
  CurrentPlotBin$LowerBound <- unlist(rm_between(CIs$Y, '=[', ',', extract=TRUE))
  CurrentPlotBin$UpperBound <- unlist(rm_between(CIs$Y, ',', ']', extract=TRUE))
  
  return(CurrentPlotBin)
}

SexismLevel <- seq(0,100,10)

for(i in 1:length(SexismLevel))
{
  CurrentFileName <- paste("Scripts/Figures/Estimates/Racial Resentment/Congress-Sexism-",SexismLevel[i],".csv", sep="")
  CurrentFile <- read.csv(CurrentFileName)
  CurrentPlotBin <- CreatePlotBin(CurrentFile, SexismLevel[i])  
  if(i==1){RunningPlotBin <- CurrentPlotBin}
  if(i>1){RunningPlotBin <- rbind(RunningPlotBin, CurrentPlotBin)}
}

####
# Create Plot
###

CurrentData <- RunningPlotBin

windowsFonts(
  A=windowsFont("Arial Black"),
  B=windowsFont("Cambria"),
  C=windowsFont("Comic Sans MS"),
  D=windowsFont("Symbol")
)

tiff(filename = "Figures/Appendix/Figure-A-6-LeftPanel-Sexism.tiff", width = 4, height = 3.5, units = "in", pointsize = 12, res=1000)
par(
  mai=c(.5,.75,.25,.1),
  family="B")

customgrey <- rgb(150, 150, 150, alpha=80, maxColorValue=255, names="customgrey")

plot(CurrentData$Sexism, CurrentData$Estimate,
     type="n",
     xlab="",
     ylab="",
     ylim=c(-.1, .1),
     xlim=c(0,1),
     family="B",
     xaxt="n",
     yaxt="n",
     yaxs = "i", # Removes Inner Margin
     xaxs = "i", # Removes Inner Margin
)

polygon(c(CurrentData$Sexism, rev(CurrentData$Sexism)), c(CurrentData$UpperBound, rev(CurrentData$LowerBound)), col=customgrey, border="NA")
lines(CurrentData$Sexism, CurrentData$Estimate, lwd=2)
abline(h=0)

# Title
#title(main="Efficacy towards Congress", line=.5, cex.main=.75)

# Axes
axis(2, at=0, labels=c("Change in Efficacy Measure"), line=1.4, cex.axis=.75, tick=FALSE, family="B")
axis(2, at=c(-.1,-.05,0,.05,.1), labels=c("-0.10", "-0.05", "0.00", "0.05", "0.10"), cex.axis=.75, family="B", las=1)

axis(1, at=c(0, .2,.4,.6,  .8,  1.0),labels=c("", "","","", "","")) # Add tick marks
axis(1, at=c(0, .2,.4,.6,  .8,  1.0), labels=c("0", "0.2","0.4","0.6", "0.8","1.0"), cex.axis=.75, las=1, line=-.75, tick=FALSE)
axis(1, at=.5, labels=c("Sexism Value"), line=.25, cex.axis=.75, tick=FALSE)


dev.off()

###
# Racial Resentment
###

CreatePlotBin <- function(CurrentFile, RR)
{
  colnames(CurrentFile) <- c("X", "Y")
  Estimates <- subset(CurrentFile, CurrentFile$X!="=")
  
  CIs <- subset(CurrentFile, CurrentFile$X=="=")
  LowerBounds <- unlist(rm_between(CIs$Y, '=[', ',', extract=TRUE))
  UpperBounds <- unlist(rm_between(CIs$Y, ',', ']', extract=TRUE))
  
  CurrentPlotBin <- data.frame(matrix(NA, 1,0))
  CurrentPlotBin$RR <- RR/100
  CurrentPlotBin$Estimate <- head(Estimates$Y,1)
  CurrentPlotBin$Estimate <- as.numeric(gsub("=", "", CurrentPlotBin$Estimate))
  CurrentPlotBin$LowerBound <- unlist(rm_between(CIs$Y, '=[', ',', extract=TRUE))
  CurrentPlotBin$UpperBound <- unlist(rm_between(CIs$Y, ',', ']', extract=TRUE))
  
  return(CurrentPlotBin)
}

RRLevel <- seq(0,100,10)

for(i in 1:length(RRLevel))
{
  CurrentFileName <- paste("Scripts/Figures/Estimates/Racial Resentment/Congress-RacialResentment-",RRLevel[i],".csv", sep="")
  CurrentFile <- read.csv(CurrentFileName)
  CurrentPlotBin <- CreatePlotBin(CurrentFile, RRLevel[i])  
  if(i==1){RunningPlotBin <- CurrentPlotBin}
  if(i>1){RunningPlotBin <- rbind(RunningPlotBin, CurrentPlotBin)}
}

####
# Create Plot
###

CurrentData <- RunningPlotBin

windowsFonts(
  A=windowsFont("Arial Black"),
  B=windowsFont("Cambria"),
  C=windowsFont("Comic Sans MS"),
  D=windowsFont("Symbol")
)

tiff(filename = "Figures/Appendix/Figure-A-6-RightPanel-RacialResentment.tiff", width = 4, height = 3.5, units = "in", pointsize = 12, res=1000)
par(
  mai=c(.5,.75,.25,.1),
  family="B")

customgrey <- rgb(150, 150, 150, alpha=80, maxColorValue=255, names="customgrey")

plot(CurrentData$RR, CurrentData$Estimate,
     type="n",
     xlab="",
     ylab="",
     ylim=c(-.1, .1),
     xlim=c(0,1),
     family="B",
     xaxt="n",
     yaxt="n",
     yaxs = "i", # Removes Inner Margin
     xaxs = "i", # Removes Inner Margin
)

polygon(c(CurrentData$RR, rev(CurrentData$RR)), c(CurrentData$UpperBound, rev(CurrentData$LowerBound)), col=customgrey, border="NA")
lines(CurrentData$RR, CurrentData$Estimate, lwd=2)
abline(h=0)

# Title
#title(main="Efficacy towards Congress", line=.5, cex.main=.75)

# Axes
axis(2, at=0, labels=c("Change in Efficacy Measure"), line=1.4, cex.axis=.75, tick=FALSE, family="B")
axis(2, at=c(-.1,-.05,0,.05,.1), labels=c("-0.10", "-0.05", "0.00", "0.05", "0.10"), cex.axis=.75, family="B", las=1)

axis(1, at=c(0, .2,.4,.6,  .8,  1.0),labels=c("", "","","", "","")) # Add tick marks
axis(1, at=c(0, .2,.4,.6,  .8,  1.0), labels=c("0", "0.2","0.4","0.6", "0.8","1.0"), cex.axis=.75, las=1, line=-.75, tick=FALSE)
axis(1, at=.5, labels=c("Racial Resentment Value"), line=.25, cex.axis=.75, tick=FALSE)


dev.off()

